﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Text;

namespace PageAdmin.Utils
{
    /// <summary>
    /// Json帮助类
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// json反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(string json)
        {
            JsonSerializerSettings jsSettings = new JsonSerializerSettings();
            jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            jsSettings.Converters.Add(new IsoDateTimeConverter
            {
                DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
            });
            return JsonConvert.DeserializeObject<T>(json, jsSettings);
        }


        /// <summary>
        ///  对象序列化成json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeObject<T>(T obj) where T : class
        {
            if (obj == null)
            {
                return null;
            }
            //日期和间都管用  
            JsonSerializerSettings jsSettings = new JsonSerializerSettings();
            jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            jsSettings.Converters.Add(new IsoDateTimeConverter
            {
                DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
            });
            return JsonConvert.SerializeObject(obj, jsSettings);
        }

        /// <summary>
        ///  对象转字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToString(object obj) 
        {
            if (obj is string)
            {
                return obj.ToString();
            }
            if (obj == null)
            {
                return null;
            }
            //日期和间都管用  
            JsonSerializerSettings jsSettings = new JsonSerializerSettings();
            jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            jsSettings.Converters.Add(new IsoDateTimeConverter
            {
                DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
            });
            return JsonConvert.SerializeObject(obj, jsSettings);
        }


        /// <summary>
        ///  对象序列化成json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string JsonParse<T>(T obj) where T : class
        {
            return SerializeObject<T>(obj);
        }


        /// <summary>
        /// 判断是否是json字符串，仅仅大致判断格式如{开头，}结尾或[开头,]结尾
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static bool IsJson(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return false;
            }
            else if (json.StartsWith("{") && json.EndsWith("}") && json.IndexOf("\"") > 0)
            {
                return true;
            }
            else if (json.StartsWith("[") && json.EndsWith("]") && json.IndexOf("\"") > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// json实现深拷贝
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T DeepClone<T>(T obj) where T : class
        {
            string json = JsonParse<T>(obj);
            return DeserializeObject<T>(json);
        }

        private static string JsonFormat(string str, Type type)
        {
            if (string.IsNullOrEmpty(str))
            {
                str = "";
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.Length; i++)
            {
                char c = str.ToCharArray()[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\""); break;
                    case '\\':
                        sb.Append("\\\\"); break;
                    case '/':
                        sb.Append("\\/"); break;
                    case '\b':
                        sb.Append("\\b"); break;
                    case '\f':
                        sb.Append("\\f"); break;
                    case '\n':
                        sb.Append("\\n"); break;
                    case '\r':
                        sb.Append("\\r"); break;
                    case '\t':
                        sb.Append("\\t"); break;
                    default:
                        sb.Append(c); break;
                }
            }
            str = sb.ToString();
            str = "\"" + str + "\"";
            return str;

        }

    }
}